<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="选择店铺：" prop="chooseStoreId">
          <el-select v-model="chooseStoreId" placeholder="请选择店铺" class="width4" @change="getCookie" filterable>
            <el-option v-for="(item,index) in storeList" :key="index"
                       :label="getStoreLabel(item)"
                       :value="item.value || item.storeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="tradeOrderId">
          <el-input v-model="form.tradeOrderId" size="small" placeholder="请输入订单号">
            <template slot="append">
              <el-button :loading="butLoading" type="text" class="padding10" @click="getOrderLogisticsInfo">
                查看物流单号
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="原物流号" prop="oldMailNo">
          <el-input v-model="form.oldMailNo" size="small" placeholder="请输入原物流单号"></el-input>
        </el-form-item>
        <el-form-item label="新物流号" prop="newMailNo">
          <el-input v-model="form.newMailNo" size="small" placeholder="请输入新物流单号"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {queryCookieInd} from "@/api/shop/index";
import {changeOfflineLogisticsOrder, queryOrderLogisticsInfo} from "@/api/autoOperation/python";
import {sendOutOrderToFPX, purchaseCreateLogisticsNumber} from "@/api/order/orderApi";

export default {
  props: {
    storeId: {
      default: "",
      type: String
    },
    orderId: {
      default: "",
      type: String / Number
    },
    offlineLogisticsType: { // 0 :编写，1：修改
      default: "",
      type: String
    },
    storeList: {
      default: [],
      type: Array
    }
  },
  data() {
    return {
      buttonLoading: false,//确定提交表单按钮loading
      title: "编辑线下物流单号",
      open: true,
      form: {},
      chooseStoreId: this.storeId,
      rules: {
        tradeOrderId: [{required: true, message: "原订单号不能为空", trigger: "blur"}],
        oldMailNo: [{required: true, message: "原物流号不能为空", trigger: "blur"}],
        newMailNo: [{required: true, message: "新物流号不能为空", trigger: "change"},],
        cookie_str: [{required: true, message: "cookie不能为空", trigger: "change"}],
      },
      butLoading: false,//查看物流单号loading
    }
  },
  created() {
    this.reset()
    if (this.chooseStoreId) {
      this.getCookie()
    }
  },
  computed: {
    // 获取店铺选择列表的标签label
    getStoreLabel() {
      return item => {
        // console.log('item', item)
        let storeLabel = item.storeUserName ? item.storeUserName : item.storeAlias
        return storeLabel + ' - ' + item.storeName
      }
    }
  },
  methods: {
    getOrderLogisticsInfo() {
      if (!this.form.cookie_str) {
        this.$message.error('请先选择店铺！')
        return;
      }
      if (!this.form.tradeOrderId) {
        this.$message.error('请输入订单号！')
        return;
      }
      this.butLoading = true
      let data = {
        cookie_str: this.form.cookie_str,
        orderId: this.form.tradeOrderId
      }
      queryOrderLogisticsInfo(data).then(res => {
        this.butLoading = false;
        this.$confirm(res.data.toString(), '物流单号')
      })
    },
    // 获取cookie
    getCookie() {
      queryCookieInd({storeId: this.chooseStoreId}).then(res => {
        let data = res.data
        if (data) {
          this.form.cookie_str = res.data.cookie
        } else {
          this.$message.error('该店铺尚未绑定cookie！')
        }
      })
    },
    // 确定提交
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.buttonLoading = true
          let type = this.offlineLogisticsType
          changeOfflineLogisticsOrder(this.form).then(res => {
            if (type) {
              if (type == '1') { //采购订单修改单号
                this.autoSendGodds()
              } else {
                this.purchaseCreateLogisticsNumber()
              }
            } else { // 运营编辑单号
              this.$message.success(res.msg)
            }
          }).finally((err) => {
            this.buttonLoading = false
          })
        }
      })
    },
    // 修改订单状态
    purchaseCreateLogisticsNumber() {
      purchaseCreateLogisticsNumber({orderId: this.orderId}).then(res => {
        this.$message.success(res.msg)
        this.cancel('isRef')
      })
    },
    // 自动发货
    autoSendGodds() {
      let data = {
        orderId: this.orderId,
        type: "0"
      }
      sendOutOrderToFPX(data).then(res => {
        this.$message.success(res.msg)
        this.cancel('isRef')
      })
    },
    reset() {
      this.form = {
        // tradeOrderId: '8182663145801551',
        // oldMailNo: '2356713954',
        // newMailNo: 'UJ235671395YP',
        cookie_str: undefined,
        tradeOrderId: this.orderId,
        oldMailNo: this.offlineLogisticsType == '0' ? '0' : undefined,
        newMailNo: this.offlineLogisticsType == '0' ? this.$getRandomNum(10) : undefined,
        contry_name: ""
      }
      this.resetForm("form");
    },

    // 取消
    cancel(e) {
      this.$emit('close', e)
    },
  }
}
</script>

<style scoped>

</style>
